<template>
  <el-dialog title="选择用户" :visible.sync="visible" width="40%" @close="$emit('update:visible', false)">
    <!--条件搜索表单-->
    <el-row :gutter="20">
      <el-form :inline="true">
        <el-form-item>
          <el-input v-model="searchObj.username" placeholder="用户名称" />
        </el-form-item>
        <el-form-item>
          <el-input v-model="searchObj.email" placeholder="用户邮箱" />
        </el-form-item>
        <el-form-item>
          <el-input v-model="searchObj.phone" placeholder="手机号码" />
        </el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="getAll()">查询</el-button>
      </el-form>
    </el-row>

    <!-- banner列表 :data="list"表示遍历list数组 stripe条纹 -->
    <el-table
      v-loading="loading"
      element-loading-text="正在努力加载数据中..."
      :data="page.records"
      :border="false"
      stripe
      highlight-current-row
      style="width: 100%; margin: 15px 0px"
      @row-click="handleRowClick"
    >
      <el-table-column fixed="left" width="55">
        <template slot-scope="scope">
          <el-radio v-model="selectionId" class="radio" :label="scope.row.id"><span style="display: none" /> </el-radio>
        </template>
      </el-table-column>
      <el-table-column prop="username" fixed="left" label="用户名称" min-width="100" sortable show-overflow-tooltip />
      <el-table-column prop="nickname" label="用户昵称" show-overflow-tooltip />
      <el-table-column prop="sex" label="用户性别" min-width="100" :filters="sexOptions" :filter-method="filterHandler">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.sex === 0" type="success">男</el-tag>
          <el-tag v-if="scope.row.sex === 1" type="danger">女</el-tag>
          <el-tag v-if="scope.row.sex === 2" type="info">未知</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="phone" label="手机号码" min-width="100" sortable show-overflow-tooltip />
      <el-table-column prop="createTime" label="创建时间" min-width="100" sortable show-overflow-tooltip />
    </el-table>

    <!-- 分页导航栏 -->
    <div>
      <el-pagination
        :background="true"
        :current-page="pIndex"
        :page-size="pSize"
        :total="page.total"
        :page-sizes="[1, 3, 6, 9, 12, 15, 18]"
        layout="total, sizes, ->, prev, pager, next, jumper"
        @current-change="getAll"
        @size-change="handleSizeChange"
      />
    </div>

    <div slot="footer">
      <el-button @click="$emit('update:visible', false)">取 消</el-button>
      <el-button type="primary" @click="submit()">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
// 引入api接口定义的js文件
import userApi from '@/api/sys/user'

export default {
  props: {
    visible: { type: Boolean, required: true, default: false },
    userId: { type: Number, required: false, default: 0 }
  },
  data() {
    // 定义变量和初始值
    return {
      // 性别选项
      sexOptions: [
        { value: 0, text: '男' },
        { value: 1, text: '女' },
        { value: 2, text: '未知' }
      ],
      // 查询条件
      searchObj: {},

      // 列表相关
      loading: false, // 加载状态
      pIndex: 1, // 当前页
      pSize: 6, // 每页显示记录数
      page: [], // 列表
      selectionId: 1,
      selectionRow: {}
    }
  },

  watch: {
    visible(newVal, oldVal) {
      if (newVal !== oldVal) {
        this.selectionId = this.userId
        this.getAll()
      }
    }
  },

  methods: {
    // 加载列表数据
    getAll(current = 1) {
      this.loading = true
      this.pIndex = current
      userApi
        .getPage(this.pIndex, this.pSize, this.searchObj)
        .then((response) => {
          this.page = response.data
        })
        .finally(() => {
          this.loading = false
        })
    },

    // 行内筛选
    filterHandler(value, row, column) {
      const property = column['property']
      return row[property] === value
    },

    // 改变页码
    handleSizeChange(pageSize) {
      this.pSize = pageSize
      // 重新调用数据加载方法 --> 方法中已经加入的查询参数
      this.getAll()
    },

    // 单击选中
    handleRowClick(row) {
      this.selectionId = row.id
      this.selectionRow = row
    },

    // 重置
    reset() {
      this.searchObj = {}
      this.getAll()
    },

    // 提交
    submit() {
      this.$emit('getSelectUser', this.selectionRow)
      this.$emit('update:visible', false)
    }
  }
}
</script>

<style></style>
